package com.etc.ui;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.etc.dao.DeptDao_HashMap;
import com.etc.entity.Dept;
import com.etc.service.DeptService;

public class DeptManage_UI_DisplayDept extends JFrame {

	private JPanel contentPane;
	private static JTable table;

	DeptDao_HashMap dh = new DeptDao_HashMap();
	private JScrollPane scrollPane;
	private JTextField textField;
	// 右键菜单
	private JPopupMenu popupMenu;
	// 三个菜单项
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;

	// 全局的DeptNo
	static String deptNo = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DeptManage_UI_DisplayDept frame = new DeptManage_UI_DisplayDept();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DeptManage_UI_DisplayDept() {
		setTitle("部门显示");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 725, 505);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {

		});
		scrollPane.setBounds(68, 134, 580, 219);
		contentPane.add(scrollPane);

		table = new JTable();
		// 这一行代码是给表格插入数据[列 行]
		table.setModel(new DefaultTableModel(
				// 二维存放的数据的行
				new Object[][] { { "1", "\u8D22\u52A1\u90E8", "\u53A6\u95E8" }, { null, null, null }, },
				// 存放的是标题
				new String[] { "\u7F16\u53F7", "\u90E8\u95E8\u540D\u79F0", "\u90E8\u95E8\u5730\u5740" }));
		scrollPane.setViewportView(table);

		popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		mntmNewMenuItem = new JMenuItem("add");
		popupMenu.add(mntmNewMenuItem);

		mntmNewMenuItem_1 = new JMenuItem("update");
		popupMenu.add(mntmNewMenuItem_1);

		mntmNewMenuItem_2 = new JMenuItem("delete");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 删除
				System.out.println("delete deptNo :" + deptNo);
				// 此处去调用Service 实现删除操作
				// 省略 .....

				DeptService ds = new DeptService();
				boolean flag = ds.delDept(Integer.parseInt(deptNo));
				if (flag == true) {
					JOptionPane.showMessageDialog(null, "编号为 [" + deptNo + "] 部门 ,删除成功");
					//刷新界面的显示
					
				}
				else
				{
					JOptionPane.showMessageDialog(null, "编号为 [" + deptNo + "] 部门 ,删除失败");
				}

			}
		});
		popupMenu.add(mntmNewMenuItem_2);

		JButton btnShow = new JButton("显示部门");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// 和数据库表关联在一起
				HashMap<Integer, Dept> hashMap = dh.getDept();
				// 创建一个保存了数据库表中行集合的数组
				Object[][] objs = new Object[hashMap.size()][3];

				// 此处输出结果
				Iterator<Integer> itor = hashMap.keySet().iterator();

				for (int i = 0; itor.hasNext(); i++) {
					// 得到key
					Integer key = itor.next();
					// 这里要得到dept对象
					Dept dept = hashMap.get(key);

					// 将数据存储在new Object[][]的一个二维数组中
					int deptNo = dept.getDeptNo();
					String deptName = dept.getDeptName();
					String deptAddress = dept.getDeptAddress();

					// 每次赋值之前要对第二个维度进行创建
					// objs[i] = new Object[3];
					// 给数组的一行赋值
					objs[i][0] = deptNo;
					objs[i][1] = deptName;
					objs[i][2] = deptAddress;

				}

				// 循环执行之后,获取到了部门的数据

				// 这一行代码是给表格插入数据[列 行]
				table.setModel(new DefaultTableModel(
						// 二维存放的数据的行
						objs,
						// 存放的是标题
						new String[] { "\u7F16\u53F7", "\u90E8\u95E8\u540D\u79F0", "\u90E8\u95E8\u5730\u5740" }));
				// 别忘记了
				scrollPane.setViewportView(table);

			}
		});
		btnShow.setBounds(447, 81, 93, 23);
		contentPane.add(btnShow);

		textField = new JTextField();
		textField.setBounds(222, 81, 170, 23);
		contentPane.add(textField);
		textField.setColumns(10);
	}

	/**
	 * 自动生成的方法 addPopup
	 * 
	 * @param component
	 * @param popup
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {

			// mousePressed 鼠标按下
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}

				// 这里就是 鼠标按下事件,但是我们只处理右键按下去的事件
				// 右键得到的结果 e.getButton() == 3
				// e.getButton() == MouseEvent.BUTTON3
				// System.out.println("mousePressed :"+e.getButton());
				if (e.getButton() == MouseEvent.BUTTON3) {
					// 此时设置jtable选中的行的位置,右键点击在那个位置,行就选中那个
					// 鼠标的位置的获取
					// e.getPoint() 鼠标位置的点
					// table.rowAtPoint 行定位
					table.setRowSelectionInterval(table.rowAtPoint(e.getPoint()), table.rowAtPoint(e.getPoint()));

					// 得到第一列的值
					deptNo = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";

					// System.out.println("deptNo :" + deptNo);
				}

			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
